﻿
// MFCTestDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "MFCTest.h"
#include "MFCTestDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCTestDlg 对话框



CMFCTestDlg::CMFCTestDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MFCTEST_DIALOG, pParent)
	, m_pPic(nullptr)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bMove = TRUE;
}

CMFCTestDlg::~CMFCTestDlg()
{
	if (m_pPic) {
		m_pPic->Stop();
		m_pPic->DestroyWindow();
		delete m_pPic;
	}
	Shell_NotifyIcon(NIM_DELETE, &m_icon);
}

void CMFCTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_PIC_GIF, (CWnd&)*m_pPic);
}

BEGIN_MESSAGE_MAP(CMFCTestDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_WM_NCHITTEST()

	ON_MESSAGE(WM_ICONMESSAGE, OnShowTrayMenu)
	ON_COMMAND(ID_MENU_CLOSE, &CMFCTestDlg::OnMenuClose)
END_MESSAGE_MAP()


// CMFCTestDlg 消息处理程序

BOOL CMFCTestDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW, 1);
	m_pPic = new CPictureEx;
	m_pPic->Create(L"gif", WS_CHILD | WS_VISIBLE, CRect(0, 0, 100, 100), this, IDC_PIC_GIF);
	// 加载所有gif
	m_vecGif.push_back(IDR_GIF1);
	m_vecGif.push_back(IDR_GIF2);
	m_vecGif.push_back(IDR_GIF3);
	m_vecGif.push_back(IDR_GIF4);
	m_vecGif.push_back(IDR_GIF5);
	m_vecGif.push_back(IDR_GIF6);
	m_vecGif.push_back(IDR_GIF7);
	m_vecGif.push_back(IDR_GIF8);
	m_vecGif.push_back(IDR_GIF9);
	m_vecGif.push_back(IDR_GIF10);
	// 定时器
	m_nIndex = 0;
	// 3秒切换一次
	SetTimer(88, 3000, NULL);
	MoveRight();
	CreateTrayIcon();

	// 设置窗口透明
	::SetWindowLong(m_hWnd,GWL_EXSTYLE,GetWindowLong(m_hWnd, GWL_EXSTYLE) ^ WS_EX_LAYERED /*^ WS_EX_TRANSPARENT*/);
	::SetLayeredWindowAttributes(m_hWnd, GetSysColor(COLOR_BTNFACE), 120, LWA_COLORKEY | LWA_ALPHA);
	// 播放音乐
	PlayMp3();
	PostMessage(WM_TIMER, 88, NULL);

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CMFCTestDlg::MoveRight()
{
	RECT rtWorkArea;
	SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)&rtWorkArea, 0);
	// 获取屏幕大小（包括任务栏）
	int cx = GetSystemMetrics(SM_CXSCREEN);
	int cy = GetSystemMetrics(SM_CYSCREEN)-50;
	CRect rcClient;
	GetClientRect(&rcClient);
	int nCx = (cx-rcClient.Width());
	int nCy = (cy-rcClient.Height());
	MoveWindow(nCx,nCy,rcClient.Width(),rcClient.Height());
	SetParent(GetDesktopWindow());
	SetOwner(GetDesktopWindow());
	//::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, rt.right, rt.bottom, SWP_SHOWWINDOW);
}

void CMFCTestDlg::CreateTrayIcon()
{
	memset((void*)&m_icon, 0, sizeof(NOTIFYICONDATAA));
	m_icon.cbSize = sizeof(NOTIFYICONDATAA);
	m_icon.hWnd = this->GetSafeHwnd();
	//strcpy_s(m_icon.szTip, "托盘菜单提示信息");
	wcscpy_s(m_icon.szTip,L"小红帽-洛天依");
	m_icon.uCallbackMessage = WM_ICONMESSAGE;
	m_icon.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
	m_icon.hIcon = m_hIcon;
	m_icon.uID = IDR_MAINFRAME;
	Shell_NotifyIcon(NIM_ADD, &m_icon);
}

LRESULT CMFCTestDlg::OnShowTrayMenu(WPARAM wp, LPARAM lp)
{
	if (lp == WM_LBUTTONDOWN || lp == WM_LBUTTONDBLCLK) {
		ShowWindow(SW_SHOW);
	}
	else if (lp == WM_RBUTTONUP)
	{
		CMenu menu;
		menu.LoadMenuW(IDR_MENU1);
		CPoint point;
		GetCursorPos(&point);
		SetForegroundWindow();
		menu.GetSubMenu(0)->TrackPopupMenu(
			TPM_LEFTBUTTON | TPM_LEFTALIGN,
			point.x, point.y, this, NULL);
		PostMessage(WM_USER, 0, 0);
	}
	return 1;
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMFCTestDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
		//DrawGif();
		//DrawGif2();
	}
}

void CMFCTestDlg::ListFiles(vector<CString> &files)
{
	WCHAR szCurrentPath[1024] = {0};
	GetModuleFileName(NULL,szCurrentPath,1024);
	for (int i = wcslen(szCurrentPath) - 1; i > 0; i--) {
		if (szCurrentPath[i] == L'\\' || szCurrentPath[i] == L'/') {
			szCurrentPath[i + 1] = 0;
			break;
		}
	}
	CString fullPath = szCurrentPath;
	fullPath += "gif/*.gif";
	AfxMessageBox(fullPath);
	CFileFind fileDir;
	BOOL bFind = fileDir.FindFile(fullPath);
	while (bFind)
	{
		bFind = fileDir.FindNextFile();
		files.push_back(fileDir.GetFileName());
	}
	for (int i = 0; i < files.size(); i++) {
		AfxMessageBox(files[i]);
	}
}

CString CMFCTestDlg::GetFullPath()
{
	WCHAR szCurrentPath[1024] = { 0 };
	GetModuleFileName(NULL, szCurrentPath, 1024);
	for (int i = wcslen(szCurrentPath) - 1; i > 0; i--) {
		if (szCurrentPath[i] == L'\\' || szCurrentPath[i] == L'/') {
			szCurrentPath[i + 1] = 0;
			break;
		}
	}
	CString fullPath = szCurrentPath;
	return fullPath;
}


void CMFCTestDlg::DrawGif()
{
	if (m_pPic && m_pPic->GetSafeHwnd()) {
		//m_pPic->Stop();
		if (m_nIndex >= 0 && m_nIndex < m_vecGif.size()) {
			m_pPic->Load(MAKEINTRESOURCE(m_vecGif[m_nIndex]), L"GIF");
			m_pPic->Draw();
		}
		else {
			//AfxMessageBox(L"gif index is too large");
		}
	}
	else {
		//AfxMessageBox(L"Create Gif window fail");
	}
}

#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
void CMFCTestDlg::PlayMp3()
{
	CString MusicPath = GetFullPath();
	MusicPath += L"music.mp3";
	CString strCommond = L"play ";
	strCommond += MusicPath;
	strCommond += " repeat";
	mciSendString(strCommond, NULL, 0, NULL);
	return;
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFCTestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CMFCTestDlg::OnTimer(UINT_PTR nIDEvent)
{
	switch (nIDEvent)
	{
	case 88:
		srand((int)time(0));
		m_nIndex = rand() % 10;
		DrawGif();
		break;
	default:
		break;
	}

	CDialogEx::OnTimer(nIDEvent);
}


LRESULT CMFCTestDlg::OnNcHitTest(CPoint point)
{
	return HTCAPTION;
	UINT nHitTest = CDialog::OnNcHitTest(point);
	// 如果鼠标在窗口客户区，则返回标题条代号给Windows
	// 使Windows按鼠标在标题条上类进行处理，即可单击移动窗口
	return (nHitTest == HTCLIENT) ? HTCAPTION : nHitTest;
	return CDialogEx::OnNcHitTest(point);
}


void CMFCTestDlg::OnMenuClose()
{
	PostMessage(WM_CLOSE);
}


BOOL CMFCTestDlg::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_SYSCOMMAND)
	{
		if (pMsg->lParam == SC_MINIMIZE)
		{
			return TRUE;
		}
	}

	return CDialogEx::PreTranslateMessage(pMsg);
}
